package com.zxmr.webclient.audit;

import java.io.File;
import java.util.List;

import com.zxmr.asyn.TaskMonitor;
import com.zxmr.audit.Audit;
import com.zxmr.audit.AuditScheduleCallBack;
import com.zxmr.domain.file.DeleteFile;
import com.zxmr.domain.service.ResourceService;

public class ResourceAudit extends Audit{

	long timeout = 24 * 60 * 60 * 1000;
	TaskMonitor monitor;
	public ResourceAudit(TaskMonitor monitor)
	{
		this.monitor = monitor;
	}
	static String root = ResourceAudit.class.getClassLoader().getResource("").getPath()
				+".."+File.separator+"upload";
	@Override
	public void audit() {
		
		//schedule next time audit
		AuditScheduleCallBack callback = new AuditScheduleCallBack(this);
		monitor.addTask(callback, timeout);
		
		
		ResourceService service = new ResourceService();
		List<DeleteFile> files = service.queryDeleteFile();
		
		if(files != null && !files.isEmpty())
		{
			for(DeleteFile file : files)
			{
				removeFile(file);
			}
		}
		
	}
	private void removeFile(DeleteFile file)
	{
		System.out.println("going to delete file "+file.getUrl());
		String url = root + file.getUrl();
		
		File f = new File(url);
		if(f.isFile() && f.exists())
		{
			f.delete();
		}
		
		ResourceService service = new ResourceService();
		service.deleteDeleteFile(file);
	}

}
